package uk.ac.manchester.cs.haphan.modbench;

import java.io.*;
import java.util.*;
import org.semanticweb.owlapi.apibinding.*;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.reasoner.*;
import org.semanticweb.owlapi.util.*;
import uk.ac.manchester.cs.factplusplus.owlapiv3.FaCTPlusPlusReasonerFactory;

public class BenchmarkWorker implements Runnable {

    private OWLAxiom entailment;
    private Set<File> allExp;
    private int counter;

    public BenchmarkWorker(Object parameters) {
        ArrayList p = (ArrayList) parameters;
        this.entailment = (OWLAxiom) p.get(0);
        this.allExp = (Set<File>) p.get(1);
        this.counter = (Integer) p.get(2);
    }

    public void run() {
        System.out.println("\tThread Started");
        System.out.println(this.counter);
        this.BenchmarkEntailment(this.entailment, this.allExp);
        System.out.println("\tThread Ended");
    }

    public void BenchmarkEntailment(OWLAxiom entailment, Set<File> allExp) {
        OWLOntology expOnt = null;

        // for each explanation create a new RF for three reasoners and measure entailment check for each reasoner
        for (File exp : allExp) {
            //System.out.println("    EXPLANATION: " + exp);
            try {

                boolean isEn = false;
                OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
                expOnt = manager.loadOntologyFromOntologyDocument(exp);


                DLExpressivityChecker exChecker = new DLExpressivityChecker(Collections.singleton(expOnt));
                String DLEx = exChecker.getDescriptionLogicName();
                //bmstats.addOntoInfo(expCount, entailment.toString(), expOnt.getLogicalAxiomCount(), DLEx, this.countOntology(expOnt));

                int run = 1000;
                while (run > 0) {
                    OWLReasonerFactory factRf = new FaCTPlusPlusReasonerFactory();

                    OWLReasoner reasoner = factRf.createReasoner(expOnt);
                    if (reasoner.isConsistent()) {
                        isEn = reasoner.isEntailed(entailment);
                    } else {
                    }

                    reasoner.dispose();
                    reasoner = null;
                    factRf =  null;
                    run--;
                }


            } catch (OWLOntologyCreationException e) {
                System.err.println(e.getMessage());
            }
        }
    }
}
